Socket
Socket
Sign inDemoInstall

@solana/spl-token

Package Overview
Dependencies
6
Maintainers
10
Versions
46
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

@solana/spl-token


Version published
Maintainers
10
Created

Package description

What is @solana/spl-token?

@solana/spl-token is an npm package that provides a set of tools and utilities for interacting with the Solana blockchain's SPL Token program. This package allows developers to create, manage, and interact with SPL tokens, which are the standard tokens on the Solana blockchain.

What are @solana/spl-token's main functionalities?

Create a New Token

This feature allows you to create a new SPL token on the Solana blockchain. The code sample demonstrates how to set up a connection to the Solana network, generate keypairs for the payer, mint authority, and freeze authority, and then create a new token with specified decimal places.

const { Connection, Keypair } = require('@solana/web3.js');
const { createMint } = require('@solana/spl-token');

(async () => {
  const connection = new Connection('https://api.mainnet-beta.solana.com');
  const payer = Keypair.generate();
  const mintAuthority = Keypair.generate();
  const freezeAuthority = Keypair.generate();

  const mint = await createMint(
    connection,
    payer,
    mintAuthority.publicKey,
    freezeAuthority.publicKey,
    9 // Decimals
  );
  console.log('Created new token:', mint.toBase58());
})();

Create a Token Account

This feature allows you to create a new token account for a specific SPL token. The code sample demonstrates how to set up a connection, generate keypairs for the payer, mint, and owner, and then create a new token account associated with the specified mint and owner.

const { Connection, Keypair } = require('@solana/web3.js');
const { createAccount } = require('@solana/spl-token');

(async () => {
  const connection = new Connection('https://api.mainnet-beta.solana.com');
  const payer = Keypair.generate();
  const mint = Keypair.generate(); // Assume this is your token mint
  const owner = Keypair.generate();

  const tokenAccount = await createAccount(
    connection,
    payer,
    mint.publicKey,
    owner.publicKey
  );
  console.log('Created new token account:', tokenAccount.toBase58());
})();

Mint New Tokens

This feature allows you to mint new tokens to a specified token account. The code sample demonstrates how to set up a connection, generate keypairs for the payer, mint, destination account, and mint authority, and then mint a specified amount of tokens to the destination account.

const { Connection, Keypair } = require('@solana/web3.js');
const { mintTo } = require('@solana/spl-token');

(async () => {
  const connection = new Connection('https://api.mainnet-beta.solana.com');
  const payer = Keypair.generate();
  const mint = Keypair.generate(); // Assume this is your token mint
  const destination = Keypair.generate(); // Assume this is your token account
  const mintAuthority = Keypair.generate();

  await mintTo(
    connection,
    payer,
    mint.publicKey,
    destination.publicKey,
    mintAuthority,
    1000000 // Amount to mint
  );
  console.log('Minted new tokens');
})();

Transfer Tokens

This feature allows you to transfer tokens from one token account to another. The code sample demonstrates how to set up a connection, generate keypairs for the payer, source account, destination account, and owner, and then transfer a specified amount of tokens from the source account to the destination account.

const { Connection, Keypair } = require('@solana/web3.js');
const { transfer } = require('@solana/spl-token');

(async () => {
  const connection = new Connection('https://api.mainnet-beta.solana.com');
  const payer = Keypair.generate();
  const source = Keypair.generate(); // Assume this is your source token account
  const destination = Keypair.generate(); // Assume this is your destination token account
  const owner = Keypair.generate();

  await transfer(
    connection,
    payer,
    source.publicKey,
    destination.publicKey,
    owner,
    500000 // Amount to transfer
  );
  console.log('Transferred tokens');
})();

Other packages similar to @solana/spl-token

Readme

Source

Token JavaScript API

The Token JavaScript library comprises:

  • A library to interact with the on-chain program
  • A test client that exercises the program
  • Scripts to facilitate building the program

Getting Started

First fetch the npm dependencies, including @solana/web3.js, by running:

$ npm install

Select a Network

The client connects to a local Solana cluster by default.

To enable on-chain program logs, set the RUST_LOG environment variable:

$ export RUST_LOG=solana_runtime::native_loader=trace,solana_runtime::system_instruction_processor=trace,solana_runtime::bank=debug,solana_bpf_loader=debug,solana_rbpf=debug

To start a local Solana cluster run:

$ solana-test-validator

Solana cluster logs are available with:

$ solana --url http://127.0.0.1:8899/ logs

Build the on-chain program

$ npm run build:program

Run the test client

$ npm run start

Pointing to a public Solana cluster

Solana maintains three public clusters:

  • devnet - Development cluster with airdrops enabled
  • testnet - Tour De Sol test cluster without airdrops enabled
  • mainnet-beta - Main cluster

Use npm scripts to configure which cluster.

To point to devnet:

$ npm run cluster:devnet

To point back to the local cluster:

$ npm run cluster:localnet

Releasing

  1. (first-time only) Create your account on npmjs.com (with 2FA enabled!) and ask @mvines about granting the publish right and run npm login
  2. Bump version in package.json and npm install (to update package-lock.json)
  3. Create a PR for the version bump
  4. Merge the PR and push new git tag on master branch
  5. Create release on github.com from the pushed tag
  6. Run npm run build and npm publish

FAQs

Last updated on 16 Aug 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc